package com.eastfair.builder.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.eastfair.builder.service.BuilderServiceProviderService;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venuebooking.api.ServiceProviderFeign;
import com.eastfair.venueservice.dto.ExhibitionServiceProviderQuery;
import com.eastfair.venueservice.dto.ServiceProviderDTO;
import com.eastfair.venueservice.dto.ServiceProviderSpaceDTO;
import com.eastfair.venueservice.enumeration.ServiceProviderTypeEnum;
import com.eastfair.venueservice.vo.ServiceProviderSpaceVO;
import com.eastfair.venueservice.vo.ServiceProviderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 综合服务服务商Feign服务.
 *
 * @author dqq
 * @date 2022 /8/8
 */
@Service
@Slf4j
public class BuilderServiceProviderServiceImpl implements BuilderServiceProviderService {

    @Resource
    private ServiceProviderFeign serviceProviderFeign;

    @Override
    public List<ServiceProviderVO> queryExhibitionServiceProvider(ExhibitionServiceProviderQuery query) {
        log.info("queryExhibitionServiceProvider - 查询展会服务商, query={}", query);
        R<List<ServiceProviderVO>> result = serviceProviderFeign.queryExhibitionServiceProvider(query);
        log.info("queryExhibitionServiceProvider - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public List<ServiceProviderVO> listServiceProvidersByExhibitionId(Long exhibitionId, List<String> spaceCodeList, String serviceProviderType) {
        ExhibitionServiceProviderQuery query = new ExhibitionServiceProviderQuery();
        query.setExhibitionManageId(exhibitionId);
        query.setServiceProviderType(serviceProviderType);
        List<ServiceProviderSpaceDTO> serviceProviderSpaceDTOList = spaceCodeList
                .stream()
                .map(spaceCode -> {
                    ServiceProviderSpaceDTO serviceProviderSpaceDTO = new ServiceProviderSpaceDTO();
                    serviceProviderSpaceDTO.setSpaceCode(spaceCode);
                    return serviceProviderSpaceDTO;
                })
                .collect(Collectors.toList());
        query.setServiceProviderSpaces(serviceProviderSpaceDTOList);
        return queryExhibitionServiceProvider(query);
    }

    @Override
    public Map<String, List<ServiceProviderVO>> getServiceProviderVOMapByExhibitionId(Long exhibitionId, List<String> spaceCode, String serviceProviderType) {
        log.info("getServiceProviderVOMapByExhibitionId - 展会ID获取负责展馆的服务商, exhibitionId={}, spaceCode={}, serviceProviderType={}", exhibitionId, spaceCode, serviceProviderType);
        R<Map<String, List<ServiceProviderVO>>> result = serviceProviderFeign.getServiceProviderVOMapByExhibitionId(exhibitionId, spaceCode, serviceProviderType);
        log.info("getServiceProviderVOMapByExhibitionId - 展会ID获取负责展馆的服务商完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public ServiceProviderVO getHomeByExhibitionIdAndSpaceCode(Long exhibitionId, String spaceCode) {
        log.info("getHomeByExhibitionIdAndSpaceCode - 展会ID和展厅编码查询负责的主场, exhibitionId={}, spaceCode={}", exhibitionId, spaceCode);
        R<ServiceProviderVO> result = serviceProviderFeign.getHomeByExhibitionIdAndSpaceCode(exhibitionId, spaceCode);
        log.info("getHomeByExhibitionIdAndSpaceCode - 展会ID和展厅编码查询负责的主场完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public ServiceProviderVO getById(Long id) {
        log.info("getById - id查询服务商详情, id={}", id);
        R<ServiceProviderVO> result = serviceProviderFeign.getById(id);
        log.info("getById - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public ServiceProviderVO getBuilderServiceProviderByUserId(Long userId) {
        log.info("BuilderServiceProviderServiceImpl +++++ getBuilderServiceProviderByUserId - 查询搭建商服务商信息 params={}", userId);
        R<ServiceProviderVO> r = serviceProviderFeign.getByUserId(userId, ServiceProviderTypeEnum.BUILD_SERVICE_PROVIDER.getCode());
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveBuilderServiceProvider(ServiceProviderDTO serviceProviderDTO) {
        log.info("BuilderServiceProviderServiceImpl +++++ saveBuilderServiceProvider - 保存搭建商服务商信息 params={}", JSONObject.toJSON(serviceProviderDTO));
        serviceProviderDTO.setServiceProviderType(ServiceProviderTypeEnum.BUILD_SERVICE_PROVIDER.getCode());
        R<Boolean> r = serviceProviderFeign.submitServiceProvider(serviceProviderDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }

        return r.getData();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateBuilderServiceProvider(ServiceProviderDTO serviceProviderDTO) {
        log.info("BuilderServiceProviderServiceImpl +++++ updateBuilderServiceProvider - 修改搭建商服务商信息 params={}", JSONObject.toJSON(serviceProviderDTO));
        serviceProviderDTO.setServiceProviderType(ServiceProviderTypeEnum.BUILD_SERVICE_PROVIDER.getCode());
        R<Boolean> r = serviceProviderFeign.submitServiceProvider(serviceProviderDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

}